import 'dart:io';
import 'package:dio/dio.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jyofservice/YunXinGongKuang/AddWorkCondition.dart';
import 'package:jyofservice/YunXinGongKuang/WorkCondition.dart';
import 'package:jyofservice/bean/work_condition_entity.dart';
import 'package:jyofservice/bean/work_notice_detail_entity.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/util/SlideRoute.dart';

class NewsDetail extends StatefulWidget {
  String? title,stateName,neirong,enddt,ljstoptime,fanwei,createdate,realname;
  WorkConditionEntity? workConditionEntity;
  NewsDetail({this.title,this.stateName,this.neirong,this.enddt,this.ljstoptime,this.fanwei,this.createdate,this.realname,this.workConditionEntity});

  @override
  _NewsDetailState createState() => _NewsDetailState();
}

class _NewsDetailState extends State<NewsDetail> {

  WorkNoticeDetailEntity? workNoticeDetailEntity;
  String? feestate;
  String? fee;
  bool ifAdd = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init().then((value){
      switch (value) {
        case 1: //客服主管领导
          ifAdd = false;
          break;
        case 2: //客服人员
          ifAdd = true;
          break;
        case 3:
          ifAdd = false;
          break;
        case 4:
          ifAdd = true;;
          break;
      }
      getWorkInfoDetail().then((value){
        if(value!=null) {
          if(workNoticeDetailEntity!.data!.a!.type==1) {
            feestate = "已缴费";
          }else if(workNoticeDetailEntity!.data!.a!.type==2) {
            feestate="未缴费";

          }else{
            feestate ="无维修费用";
          }
          fee = workNoticeDetailEntity!.data!.a!.fee;
        }
      });
      if(mounted){
        setState(() {
        });
      }
    });

  }

  Future init() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    var postid = preferences.getInt("postid");
    return postid;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            '通知详情',
            style:
            TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(40)),
          ),
          actions: <Widget>[
            Offstage(
              offstage: ifAdd,
              child:TextButton(
                child: Icon(Icons.border_color,color: Colors.white,),
                onPressed: (){
                  if(workNoticeDetailEntity!=null) {
                    Navigator.of(context)
                        .push(SlideRoute().createRoute(AddWorkConfition(index: 1,workConditionEntity: widget.workConditionEntity,workNoticeDetailEntity: workNoticeDetailEntity,))).then((value){
                      if(value!=null&&value) {
                        Navigator.pop(context, true);
                      }
                    });
                  }

                },
              ) ,
            )
          ],
          centerTitle: true,
        ),
        body:  SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Container(
                color: Colors.white,
                padding: EdgeInsets.all(10),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(child: Text(
                          widget.title!,
                          style: TextStyle(
                              color: Colors.black,
                              fontSize: ScreenUtil().setSp(40)),
                        ))
                        ,
                        Container(
                          width: ScreenUtil().setWidth(100),
                          height: ScreenUtil().setHeight(50),
                          margin: EdgeInsets.only(left: 5),
                          decoration: BoxDecoration(
                              color: Colors.orange,
                              borderRadius:
                              BorderRadius.all(Radius.circular(5))),
                          child: Center(
                            child: Text(
                              widget.stateName!,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: ScreenUtil().setSp(35)),
                            ),
                          ),
                        )
                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setHeight(10),
                    ),
                    Row(
                      children: <Widget>[
                        Image.asset("images/person.png",
                            width: ScreenUtil().setWidth(50)),
                        Text(widget.realname!,
                            style: TextStyle(
                                color: Colors.grey,
                                fontSize: ScreenUtil().setSp(25))),
                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setHeight(10),
                    ),
                    Row(
                      children: <Widget>[
                        Image.asset(
                          "images/start.png",
                          width: ScreenUtil().setWidth(40),
                        ),
                        Text(
                            widget.createdate
                                .toString(),
                            style: TextStyle(
                                color: Colors.grey,
                                fontSize: ScreenUtil().setSp(25))),
                        SizedBox(
                          width: ScreenUtil().setWidth(10),
                        ),
                        Image.asset(
                          "images/end.png",
                          width: ScreenUtil().setWidth(40),
                        ),
                        Text(widget.enddt!,
                            style: TextStyle(
                                color: Colors.grey,
                                fontSize: ScreenUtil().setSp(25))),


                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setHeight(10),
                    ),
                    Row(
                      children: <Widget>[
                        Text('累计停暖时间：',style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(35)),),
                        Expanded(
                          flex: 1,
                          child: Text(widget.ljstoptime!,style: TextStyle(color: Colors.black87,fontSize: ScreenUtil().setSp(35)),),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setHeight(10),
                    ),
                    Row(
                      children: <Widget>[
                        Text('范围：',style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(35)),),
                        Expanded(
                          flex: 1,
                          child: Text(widget.fanwei!,style: TextStyle(color: Colors.black87,fontSize: ScreenUtil().setSp(35)),),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: ScreenUtil().setHeight(10),
                    ),
                    Container(
                      width: ScreenUtil().screenWidth,
                      padding: EdgeInsets.all(10),
                      color: Colors.white,
                      child: Text(widget.neirong!,style: TextStyle(color: Colors.black87,fontSize: ScreenUtil().setSp(35)),),
                    )

                  ],
                ),
              )
            ],
          ),
        )
    );
  }

  Future getWorkInfoDetail() async{
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "ID":widget.workConditionEntity!.iD,
      "ClientType": "phone",
      "ClientKey": aesData
    };
    print(data.toString());

    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/GetWorkInfoDetial",
        data: data);
    if(response.data["Code"]=="200") {
      workNoticeDetailEntity =WorkNoticeDetailEntity().fromJson(response.data);
    }else{
      Fluttertoast.showToast(msg: response.data["Msg"]);
    }
    return response.data;
  }

}
